package com.anjoyo.xgh.musicshare.pageview;

import java.io.File;
import java.util.ArrayList;

import m.framework.ui.widget.slidingmenu.SlidingMenu;
import android.R.menu;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.anjoyo.xgh.musicshare.R;
import com.anjoyo.xgh.musicshare.activity.DownAct;
import com.anjoyo.xgh.musicshare.activity.FavAct;
import com.anjoyo.xgh.musicshare.activity.LocalMusicAct;
import com.anjoyo.xgh.musicshare.activity.NetSongAct;
import com.anjoyo.xgh.musicshare.activity.PlayAct;
import com.anjoyo.xgh.musicshare.activity.ShareListAct;
import com.anjoyo.xgh.musicshare.application.SongApplication;
import com.anjoyo.xgh.musicshare.db.MusicDBhelper;
import com.anjoyo.xgh.musicshare.entry.Song;
import com.anjoyo.xgh.musicshare.util.Constant;

public class HomePage implements Callback, OnClickListener {
	SlidingMenu menu;
	View view;
	ImageView imageView;
	Handler handler;
	public TextView textView1,textView2,textView3;
	MusicDBhelper dBhelper;
	ArrayList<Song> songs, songs2,songs3;
	Intent intent;

	public HomePage(SlidingMenu menu) {
		this.menu = menu;
		view = LayoutInflater.from(menu.getContext()).inflate(
				R.layout.home_layout, null);
//		imageView = (ImageView) view.findViewById(R.id.iv_tou);
//		File file = new File(Constant.CACHE + "head.jpg");
//		imageView.setImageURI(Uri.fromFile(file));
		handler = new Handler(this);
		view.findViewById(R.id.layout_1).setOnClickListener(this);
		view.findViewById(R.id.layout_2).setOnClickListener(this);
		view.findViewById(R.id.layout_3).setOnClickListener(this);
		view.findViewById(R.id.layout_4).setOnClickListener(this);
		view.findViewById(R.id.layout_5).setOnClickListener(this);
		view.findViewById(R.id.iv_menu).setOnClickListener(this);
		view.findViewById(R.id.iv_music).setOnClickListener(this);
		dBhelper = new MusicDBhelper(menu.getContext());
		getdata();
	}
	
	public void getdata() {
		songs = dBhelper.getDownLoadMusic();
		songs2 = SongApplication.getInstance().LoadFavSongs();
		songs3 = SongApplication.getInstance().localsongslist;
		textView1 = (TextView) view.findViewById(R.id.tv_downnum);
		textView1.setText(songs.size() + "首歌");
		textView2 = (TextView) view.findViewById(R.id.tv_favsong);
		textView2.setText(songs2.size() + "首喜欢-"+dBhelper.getFavofflineNum()+"首已离线");
		textView3 = (TextView) view.findViewById(R.id.tv_local);
		textView3.setText(songs3.size() + "首本地歌曲");
	}
	

	public View getpage() {
		return view;
	}

	@Override
	public boolean handleMessage(Message msg) {
		return false;
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.layout_1:
			getdata();
			intent = new Intent(menu.getContext(), LocalMusicAct.class);
			intent.putExtra("songs", songs3);
			menu.getContext().startActivity(intent);
			break;
		case R.id.layout_2:
			getdata();
			intent = new Intent(menu.getContext(), FavAct.class);
			intent.putExtra("songs", songs2);
			menu.getContext().startActivity(intent);
			break;
		case R.id.layout_3:
			menu.getContext().startActivity(new Intent(menu.getContext(),NetSongAct.class));
			break;
		case R.id.layout_4:
			intent = new Intent(menu.getContext(), ShareListAct.class);
			menu.getContext().startActivity(intent);
			break;
		case R.id.layout_5:
			getdata();
			intent = new Intent(menu.getContext(), DownAct.class);
			intent.putExtra("songs", songs);
			menu.getContext().startActivity(intent);
			break;
		case R.id.iv_menu:
			if (!menu.isMenuShown()) {
				menu.showMenu();
			} else {
				menu.hideMenu();
			}
			break;
		case R.id.iv_music:
			intent= new Intent(menu.getContext(),PlayAct.class);
			menu.getContext().startActivity(intent);
			break;
		default:
			break;
		}
	}
}
